package com.atguigu.srb.core.controller.admin;

import com.atguigu.srb.common.result.R;
import com.atguigu.srb.core.mapper.UserLoginRecordMapper;
import com.atguigu.srb.core.service.UserInfoService;
import com.atguigu.srb.core.service.UserLoginRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Description:
 * @Author: Gavin
 * @Date: 7/24/2023 7:18 PM
 */
@RestController
@Api(tags = "运营管理模块")
@RequestMapping("/admin/core/statistics")
public class AdminStatisticController {
    @Resource
    UserInfoService userInfoService;
    @Resource
    UserLoginRecordService userLoginRecordService;
    @Resource
    UserLoginRecordMapper userLoginRecordMapper;
    @ApiOperation("查询用户的日登录次数和日注册次数")
    @GetMapping("/{start}/{end}")
    public R statisticsData(@PathVariable("start")String start,
                            @PathVariable("end")String end){
        //统计指定日期时间段内的登录次数
        Map<String, Object> data =  userLoginRecordService.loginAndRegistryCount(start,end);
        //统计指定日期期间段内的注册的次数

        //返回值里面需要有4个list集合
        //集合1 : loginDate
        List<Map<String,Object>> loginDatas = userLoginRecordMapper.getLoginRecords(start,end);
        //遍历集合,将集合中的日期和数量取出来,变成两个新的list
        List<Date> loginDate = new ArrayList<>();
        //集合2 : loginCounts
        List<String> loginCounts = new ArrayList<>();
        loginDatas.forEach(loginData -> {
            loginDate.add((Date)loginData.get("loginDate"));
            loginCounts.add(loginData.get("counts").toString());
        });

        //集合3 : registryDate
        Map<String ,Integer> registMap = new HashMap<>();
        registMap.put("2023-07-10",2);
        registMap.put("2023-07-13",10);
        registMap.put("2023-07-18",50);
        registMap.put("2023-07-20",3);
        //集合4 : registryCounts
        ArrayList<String> registDates = new ArrayList<>(registMap.keySet());
        ArrayList<Integer> registCounts = new ArrayList<>(registMap.values());

        return R.ok().data("loginDate",loginDate).data("loginCounts",loginCounts)
                .data("registDates",registDates).data("registCounts",registCounts);
    }
}
